package org.pcorp.battlespace.construction.ui.admin.equipement;

import java.util.ArrayList;
import java.util.List;

import javax.swing.table.AbstractTableModel;

import org.pcorp.battlespace.modele.Capacite;
import org.pcorp.battlespace.modele.Equipement;

public class ModelTableCapaciteAdmin extends AbstractTableModel {
	private static final long serialVersionUID = 1236010584307085852L;
	private List<Capacite> capacites;
	private Equipement equip;
	private List<String> entetes;

	static final int NOM=0;
	static final int CODE=1;
	static final int VALMIN=2;
	static final int VALMAX=3;

	@SuppressWarnings("unused")
	private ModelTableCapaciteAdmin() {
	}

	public ModelTableCapaciteAdmin(List<Capacite> capacites, Equipement equip) {
		this.capacites = capacites;
		this.equip = equip;
		init();
	}

	private void init() {
		entetes = new ArrayList<String>();
		int i=0;
		boolean encore=true;

		while (encore) {
			switch (i) {
				case NOM:
					entetes.add("Nom");
					break;
				case CODE:
					entetes.add("Code");
					break;
				case VALMIN:
					entetes.add("Valeur-Min");
					break;
				case VALMAX:
					entetes.add("Valeur-Max");
					break;
				default:
					encore=false;
					break;
			}
			i++;
		}

		List<Capacite> capE = equip.getCapacites();
		for (Capacite capacite : capE) {
			for (Capacite capC : capacites) {
				if (capC.getId() == capacite.getId()) {
					capC.setValeurMin(capacite.getValeurMin());
					capC.setValeurMax(capacite.getValeurMax());
					break;
				}
			}
		}
	}

	public List<Capacite> getCapacites() {
		return capacites;
	}

	public void setCapacites(List<Capacite> capacites) {
		this.capacites = capacites;
	}

	@Override
	public int getRowCount() {
		return capacites.size();
	}

	@Override
	public int getColumnCount() {
		return entetes.size();
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		Capacite capa = capacites.get(rowIndex);

		switch (columnIndex) {
			case NOM:
				return capa.getNom();
			case CODE:
				return capa.getCode();
			case VALMIN:
				return capa.getValeurMin();
			case VALMAX:
				return capa.getValeurMax();
			default:
				return null;
		}
	}

	@Override
	public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
		Capacite capa = capacites.get(rowIndex);

		switch (columnIndex) {
			case NOM:
				capa.setNom((String)aValue);
				break;
			case CODE:
				capa.setCode((String)aValue);
				break;
			case VALMIN:
				try {capa.setValeurMin(Double.parseDouble((String)aValue));}
				catch (NumberFormatException ne) {capa.setValeurMin(0);}
				break;
			case VALMAX:
				try {capa.setValeurMax(Double.parseDouble((String)aValue));}
				catch (NumberFormatException ne) {capa.setValeurMax(0);}
				break;
			default:
				return;
		}
		fireTableCellUpdated(rowIndex, columnIndex); 
	}

	@Override
	public String getColumnName(int column) {
		return entetes.get(column);
	}

	public Equipement getEquip() {
		return equip;
	}

	public void setEquip(Equipement equip) {
		this.equip = equip;
	}

	@Override
	public boolean isCellEditable(int rowIndex, int columnIndex) {
		if (columnIndex==VALMIN || columnIndex==VALMAX)
			return true;
		return false;
	}
}
